package com.prd.service.Impl;

import com.prd.config.LoginInterceptor;
import com.prd.dto.SysDepartmentLetterDTO;
import com.prd.model.SysDepartmentLetterDO;
import com.prd.mapper.SysDepartmentLetterMapper;
import com.prd.model.SysJobDescriptionDO;
import com.prd.service.SysDepartmentLetterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.prd.util.CommonUtil;
import com.prd.util.JsonData;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 部门责任书 服务实现类
 * </p>
 *
 * @author lizhaoyi
 * @since 2024-01-04
 */
@Service
public class SysDepartmentLetterServiceImpl extends ServiceImpl<SysDepartmentLetterMapper, SysDepartmentLetterDO> implements SysDepartmentLetterService {

    @Autowired
    private SysDepartmentLetterMapper sysDepartmentLetterMapper;

    /**
     * 添加部门责任书
     * @param sysDepartmentLetterDTO
     * @return
     */
    @Override
    public JsonData addDepartLetter(SysDepartmentLetterDTO sysDepartmentLetterDTO) {

        SysDepartmentLetterDO sysDepartmentLetterDO = CommonUtil.dtoTransfer(sysDepartmentLetterDTO, SysDepartmentLetterDO.class);
        sysDepartmentLetterDO.setCreateBy(LoginInterceptor.threadLocal.get().getName());
        sysDepartmentLetterDO.setCreatedTime(LocalDateTime.now());
        sysDepartmentLetterMapper.insert(sysDepartmentLetterDO);

        return JsonData.buildSuccess();
    }

    /**
     * 查看部门责任书
     * @param id
     * @return
     */
    @Override
    public JsonData detail(Long id) {
        return JsonData.buildSuccess(sysDepartmentLetterMapper.selectById(id));
    }

    /**
     * 修改部门责任书
     * @param departmentLetterDTO
     * @return
     */
    @Override
    public JsonData edit(SysDepartmentLetterDTO departmentLetterDTO) {
        SysDepartmentLetterDO letterDO = sysDepartmentLetterMapper.selectById(departmentLetterDTO.getId());
        BeanUtils.copyProperties(departmentLetterDTO,letterDO);
        int count = sysDepartmentLetterMapper.updateById(letterDO);
        return count > 0 ? JsonData.buildSuccess() : JsonData.buildError("修改部门责任书 操作异常");
    }
}
